package compte;

public class Stats {
	private Stat [] stats ;
	private Dom statsXML ;
	public Stats (){
		statsXML = new Dom("Stats", "./Datas/Stats.xml" );
	}

	public void Update(String theStatType, String theRowName, int theYear, int theMonth, Q theAmount ){

		boolean ExistingStat = false ;
		int NbStats ;
		if (stats != null )	NbStats = stats.length ;
		else 				NbStats = 0 ;

		for (int i=0;i<NbStats ;i++){
			if (stats[i].Exists(theStatType, theRowName, theYear) ) {
				ExistingStat = true ;
				stats[i].Add(theMonth, theAmount) ;
			}			
		}			

		
		if (!ExistingStat) {
			Stat newstat = new Stat ( theStatType, theRowName, theYear);
			newstat.Add(theMonth, theAmount);
			Stat [] statsBigger = new Stat [NbStats +1];
			for (int i=0;i<NbStats ;i++) statsBigger[i] = stats[i];
			statsBigger[NbStats ] = newstat ;
			stats = statsBigger ;	
		}
	}
	// 
	//
	public void Save () {
		for (int i=0;i<stats.length ;i++) stats[i].AddInTheDom(statsXML);
		statsXML.Save();
		statsXML.Transform("./Datas/Stats.xsl", "./Datas/Stats.html");
		//init again StatsXML if another Save occurs
		statsXML = new Dom("Stats", "./Datas/Stats.xml" );

	}

}
